﻿using Common.Http;
using Common.Log;
using Common.saveIma;
using HtmlAgilityPack;
using log4net;
using log4net.Config;
using log4net.Repository;
using splider.Interface;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace splider.service
{
    public class spliderService : IspliderService
    {
        //private Logger logger = new Logger(typeof(spliderService));
        private ILog logger = null;
        public spliderService()
        {
            log4net.Repository.ILoggerRepository repository = log4net.LogManager.CreateRepository("NETCoreRepository");
            var fileInfo = new FileInfo("CfgFiles/log4net.config");
            log4net.Config.XmlConfigurator.Configure(repository, fileInfo);
            log4net.Config.BasicConfigurator.Configure(repository);

            //ILoggerRepository repository = LogManager.CreateRepository("NETCoreRepository");
            //XmlConfigurator.Configure(repository, fileInfo);
            //logger = LogManager.GetLogger(repository.Name, "NETCorelog4net");

            //log4net.ILog log = log4net.LogManager.GetLogger(repository.Name, "NETCorelog4net");
            //log.Warn("ssss");
            logger = log4net.LogManager.GetLogger(repository.Name, "spider");
        }


        public void Crawl(string url)
        {
            try
            {

                string html = HttpHelper.DownloadHtml(url);
                HtmlDocument document = new HtmlDocument();
                document.LoadHtml(html);

                //if (!url.Contains("page"))//递归的跳出条件
                //{
                //    string totalPagePath = "//*[@id='J_topPage']/span/i";
                //    var node = document.DocumentNode.SelectSingleNode(totalPagePath);
                //    if (node == null)
                //        throw new Exception("J_topPage为空");
                //    int.TryParse(node.InnerHtml, out int totalPageCount);
                //    for (int i = 1; i < totalPageCount + 1; i++)
                //    {
                //        this.Crawl($"{url}&page={i}");//递归一下
                //    }
                //}


                //1是每个商品都在全部的html正则
                //2是先定位每个商品，然后再删选  效率高很多
                {
                    string commodityInfoPath = @"//*[@class='image']//a";
                    var commodityNodeArray = document.DocumentNode.SelectNodes(commodityInfoPath);
                    if (commodityNodeArray != null)
                        foreach (var node in commodityNodeArray)
                        {
                            string allHtml = node.OuterHtml;
                            HtmlDocument documentCommodity = new HtmlDocument();
                            documentCommodity.LoadHtml(allHtml);
                            string imagePath = "//a";
                            var imageNode = documentCommodity.DocumentNode.SelectSingleNode(imagePath);

                            string imageUrl = null;

                            if (imageNode != null)
                            {
                               
                                if (imageNode.Attributes["href"] != null)
                                {
                                    imageUrl = imageNode.Attributes["href"].Value;
                                }
                                //else if (imageNode.Attributes["data-lazy-img"] != null)
                                //{
                                //    imageUrl = imageNode.Attributes["data-lazy-img"].Value;
                                //}
                                else
                                {
                                    Console.WriteLine("href 为空");
                                    logger.Info("href 为空");
                                }
                              
                            }

                            string imagetitlePath = "//img";
                            string catory = null;
                            var imagetitleNode = documentCommodity.DocumentNode.SelectSingleNode(imagetitlePath);
                            if (imagetitleNode != null)
                            {
                               

                                if (imagetitleNode.Attributes["alt"] != null)
                                {
                                    catory = imagetitleNode.Attributes["alt"].Value;
                                }


                            }

                            if (imageUrl != null && !imageUrl.StartsWith("/"))
                            {
                                imageUrl = $"http://www.fotoe.com/{imageUrl}";
                                Console.WriteLine($"imageUrl={imageUrl} 类别为{catory}");
                                logger.Warn($"imageUrl={imageUrl} 类别为{catory}");
                            }

                            this.sencondCrawl(imageUrl);
                        }
                }

            }
            catch (Exception ex)
            {
                logger.Error("抓取异常", ex);
                throw;
            }
        }



        //第二层

        public void sencondCrawl( string url)
        {

            try
            {
                string html = HttpHelper.DownloadHtml(url);
                HtmlDocument document = new HtmlDocument();
                document.LoadHtml(html);

                string commodityInfoPath = @"//*[@class='image']//a";
                var commodityNodeArray = document.DocumentNode.SelectNodes(commodityInfoPath);
                if (commodityNodeArray != null)
                {
                    foreach (var node in commodityNodeArray)
                    {
                        string allHtml = node.OuterHtml;
                        HtmlDocument documentCommodity = new HtmlDocument();
                        documentCommodity.LoadHtml(allHtml);
                        string imagePath = "//a";
                        var imageNode = documentCommodity.DocumentNode.SelectSingleNode(imagePath);

                        string imageUrl = null;

                        if (imageNode != null)
                        {

                            if (imageNode.Attributes["href"] != null)
                            {
                                imageUrl = imageNode.Attributes["href"].Value;
                            }
                            else
                            {
                                Console.WriteLine("href 为空");
                                logger.Info($"href 为空");
                            }
                        }
                        string imagetitlePath = "//img";
                        string catory = null;
                        var imagetitleNode = documentCommodity.DocumentNode.SelectSingleNode(imagetitlePath);
                        if (imagetitleNode != null)
                        {
                            if (imagetitleNode.Attributes["alt"] != null)
                            {
                                catory = imagetitleNode.Attributes["alt"].Value;
                            }
                        }

                        if (imageUrl != null && !imageUrl.StartsWith("/"))
                        {
                            imageUrl = $"http://www.fotoe.com/{imageUrl}";
                            Console.WriteLine($"imageUrl={imageUrl} 类别为{catory}");

                            logger.Info($"imageUrl={imageUrl} 类别为{catory}");
                        }
                        this.thirdCrawl(imageUrl);
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }


        public void thirdCrawl(string url)
        {
            try
            {
               
                string html = HttpHelper.DownloadHtml(url);
                HtmlDocument document = new HtmlDocument();
                document.LoadHtml(html);

                string commodityInfoPath = @"//*[@class='image_group']";
                var commodityNodeArray = document.DocumentNode.SelectNodes(commodityInfoPath);
                if (commodityNodeArray != null)
                {
                    int i = 0;
                    foreach (var node in commodityNodeArray)
                    {
                        i = i + 1;
                        string allHtml = node.OuterHtml;
                        HtmlDocument documentCommodity = new HtmlDocument();
                        documentCommodity.LoadHtml(allHtml);


                        string imagePath = "//a/img";
                        var imageNode = documentCommodity.DocumentNode.SelectSingleNode(imagePath);
                        string imageUrl = null;
                        if (imageNode != null)
                        {
                            if (imageNode.Attributes["src"] != null)
                            {
                                imageUrl = imageNode.Attributes["src"].Value;
                            }
                            else
                            {
                                Console.WriteLine("href 为空");
                                logger.Info("href 为空");
                            }
                        }
                        string markPath = "//*[@class='comment'] ";
                        var markNode = documentCommodity.DocumentNode.SelectSingleNode(markPath);

                        string mark = null;
                        if (markNode != null)
                        {
                            if (markNode.InnerText != "")
                            {
                                mark = markNode.InnerText;
                            }
                            else
                            {
                                Console.WriteLine("描述为空");
                                logger.Info("描述为空");
                            }
                        }

                        Console.WriteLine($"描述为：{mark} 图片地址为 {imageUrl}");
                        var ss = imageUrl.Replace(".", "");
                        imageUrl = $"http://www.fotoe.com/{ss}";

                        ImgHelper.SaveAsWebImg(imageUrl);
                        //ImgHelper.SaveImageFromWeb(imageUrl, AppDomain.CurrentDomain.BaseDirectory + "/image/", $"test{mark.Substring(0,2)}_{i.ToString()}");
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }


        string trans(string str)
        {

            byte[] utf8bytes = System.Text.Encoding.Default.GetBytes(str);
            byte[] utf8bytes2 = System.Text.Encoding.Convert(System.Text.Encoding.Default, Encoding.UTF8, utf8bytes);
            str = System.Text.Encoding.GetEncoding("GB2312").GetString(utf8bytes2);
            return str;
        }
        string LanChange(string str)
        {
            Encoding utf8;
            Encoding gb2312;
            utf8 = Encoding.GetEncoding("UTF-8");
            gb2312 = Encoding.GetEncoding("GB2312");
            byte[] gb = gb2312.GetBytes(str);
            gb = Encoding.Convert(gb2312, utf8, gb);
            return utf8.GetString(gb);
        }

    }
}
